import turbogears           as tg
import cherrypy             as http

from turbogears             import identity, url, validators
from fasttrack.model        import Person, Project, status_codes


class DashboardController(object):
    @tg.expose(template="fasttrack.templates.dashboard.index")
    @identity.require(identity.not_anonymous())
    def index(self):
       
        all_projects = Project.select("parent_project_id is Null order by upper(name)")
        projects = [project for project in all_projects if not project.archived]
        archived_projects = [project for project in all_projects if project.archived]
        people = Person.select(orderBy='displayName')
        
        return dict(active_section='dashboard',
                    projects=projects, 
                    archived_projects=archived_projects,
                    people=people, 
                    status_codes=status_codes)
    
    
    @tg.expose()
    @tg.validate(validators=dict(contact_id=validators.Int(), subprojects=validators.Bool()))
    def new_project(self, name, description, contact_id, subprojects=False):
        contact = Person.get(contact_id)
        main_project = Project(name=name, 
                               description=description, 
                               contact=contact,
                               parent_project=None)
        if subprojects:
            dev_project = Project(name=name + ' - Development',
                                  contact=contact,
                                  description='Development sub-project for ' + name,
                                  parent_project=main_project)
            doc_project = Project(name=name + ' - Documentation',
                                  contact=contact,
                                  description='Documentation sub-project for ' + name,
                                  parent_project=main_project)
            qa_project  = Project(name=name + ' - QA',
                                  contact=contact,
                                  description='QA sub-project for ' + name,
                                  parent_project=main_project)
        
        raise http.HTTPRedirect(url('/dashboard'))